<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\model\Upload as UM;
use app\common\model\TencentVod;

class Upload extends Common
{
    private $noCheckAction = ['uploadImage', 'uploadHead', 'uploadSet'];//不需要验证的方法
    
    public function initialize(){
        parent::initialize();
        $this->doCheckAuth($this->noCheckAction);
    }
    
    public function uploadImage(){
        /*$file = $_FILES['file'];//得到传输的数据
        $dir = "upload/Official/Image/Src"; //官网
        
        $model = new UM();
        $ret = $model->_uploadImage($file, $dir);
        $root = $this->request->root(true);
        if(200 == $ret['code']){
            $thumb_dir = "upload/Official/Image/Thumb";
            $thumb = thumb($ret['des'], $thumb_dir, 100, 100);
            $image = $root.'/'.$ret['des'];
            $thumb = $root.'/'.$thumb;
            die(json_encode(array('code'=>0, 'msg'=>$ret['msg'], 'image'=>$image, 'thumb'=>$thumb)));
        }else{
            die(json_encode(array('code'=>$ret['code'], 'msg'=>$ret['msg'], 'image'=>'', 'thumb'=>'')));
        }*/
        
        $model = new UM;
        $variable = 'file';
        $controller = $this->request->controller();
        $controller = strtolower($controller);
        $dir = $controller."/"."image/article/".date("Ymd")."/".date("YmdHis").mt_rand(1000000,9999999);
        $res = $model->_uploadOssFile($variable, $dir);
        if($res){
            die(json_encode(array('code'=>0, 'msg'=>'上传成功', 'image'=>$res, 'thumb'=>'')));
        }else{
            die(outPutError($model->getError()));
        }
    }
    
    
    /**
    * 上传banner
    */
    public function uploadBanner(){
        $model = new UM;
        $variable = 'file';
        $controller = $this->request->controller();
        $controller = strtolower($controller);
        $dir = $controller."/"."image/banner/".date("Ymd")."/".date("YmdHis").mt_rand(1000000,9999999);
        $res = $model->_uploadOssFile($variable, $dir);
        if($res){
            die(json_encode(array('code'=>0, 'msg'=>'上传成功', 'image'=>$res, 'thumb'=>'')));
        }else{
            die(outPutError($model->getError()));
        }
    }
    
    /**
    * 会员上传头像
    */
    public function uploadHead(){
        $model = new UM;
        $variable = 'file';
        $controller = $this->request->controller();
        $controller = strtolower($controller);
        $dir = $controller."/"."image/head/".date("Ymd")."/".date("YmdHis").mt_rand(1000000,9999999);
        $res = $model->_uploadOssFile($variable, $dir);
        if($res){
            die(json_encode(array('code'=>0, 'msg'=>'上传成功', 'image'=>$res, 'thumb'=>'')));
        }else{
            die(outPutError($model->getError()));
        }
    }

    /**
    * 会员等级
    */
    public function uploadLevel(){
        $model = new UM;
        $variable = 'file';
        $controller = $this->request->controller();
        $controller = strtolower($controller);
        $dir = $controller."/"."image/level/".date("Ymd")."/".date("YmdHis").mt_rand(1000000,9999999);
        $res = $model->_uploadOssFile($variable, $dir);
        if($res){
            die(json_encode(array('code'=>0, 'msg'=>'上传成功', 'image'=>$res, 'thumb'=>'')));
        }else{
            die(outPutError($model->getError()));
        }
    }
    
    /**
    * 系统设置图片上传
    */
    public function uploadSet(){
        $model = new UM;
        $variable = 'file';
        $controller = $this->request->controller();
        $controller = strtolower($controller);
        $dir = $controller."/"."image/set/".date("Ymd")."/".date("YmdHis").mt_rand(1000000,9999999);
        $res = $model->_uploadOssFile($variable, $dir);
        if($res){
            die(json_encode(array('code'=>0, 'msg'=>'上传成功', 'image'=>$res, 'thumb'=>'')));
        }else{
            die(outPutError($model->getError()));
        }
    }
    
    /**
    * 系统设置图片上传
    */
    public function uploadTask(){
        $model = new UM;
        $variable = 'file';
        $controller = $this->request->controller();
        $controller = strtolower($controller);
        //var_dump($controller);die;
        $dir = $controller."/"."image/task/".date("Ymd")."/".date("YmdHis").mt_rand(1000000,9999999);
        $res = $model->_uploadOssFile($variable, $dir);
        if($res){
            die(json_encode(array('code'=>0, 'msg'=>'上传成功', 'image'=>$res, 'thumb'=>'')));
        }else{
            die(outPutError($model->getError()));
        }
    }
    
    /**
    * 系统设置图片上传
    */
    public function uploadFile(){
        $model = new UM;
        $variable = 'file';
        $controller = $this->request->controller();
        $controller = strtolower($controller);
        //var_dump($controller);die;
        $dir = $controller."/"."file/version/".date("Ymd")."/".date("YmdHis").mt_rand(1000000,9999999);
        $res = $model->_uploadOssFile($variable, $dir);
        if($res){
            die(json_encode(array('code'=>0, 'msg'=>'上传成功', 'file'=>$res)));
        }else{
            die(outPutError($model->getError()));
        }
    }
    
    
    
    /**
    * 上传视频：腾讯云
    */
    public function uploadVedio(){
        $model = new UM;
        $variable = 'file';
        $controller = $this->request->controller();
        $controller = strtolower($controller);
        //var_dump($controller);die;
        $dir = $controller."/"."vedio/".date("Ymd")."/".date("YmdHis").mt_rand(1000000,9999999);
        $res = $model->_uploadVedio($variable, $dir);
        //dump($res);die;
        
        if($res){
            die(json_encode(array('code'=>0, 'msg'=>'上传成功', 'file'=>$res['MediaUrl'], 'fid'=>$res['FileId'])));
        }else{
            die(outPutError($model->getError()));
        }
    }
    
    /**
    * 腾讯云生成签名
    */
    public function sign(){
        $model = new TencentVod;
        $signature = $model->_sign();

    }
}
